<?php

namespace app\common\model\user;

use app\common\model\BaseModel;

class UserGroup extends BaseModel
{
    /**
     * 主键
     * @var string
     */
    protected $pk = 'id';

    /**
     * 模型名称
     * @var string
     */
    protected $name = 'user_group';

    /**
     * 隐藏属性
     * @var array
     */
    protected $hidden = [

    ];

    /**
     * 只读属性
     * @var array
     */
    protected $readonly = [

    ];

    /**
     * 字段类型或者格式转换
     * @var array
     */
    protected $type = [
        'rules' => 'json',
        'menu_ids' => 'json',
        'half_menu_ids' => 'json'
    ];

    /**
     * 定义全局的查询范围
     * @var string[]
     */
    protected $globalScope = [
    ];

    public static function onBeforeWrite($userGroup){
        $userGroup->rules  = array_merge($userGroup->menu_ids,$userGroup->half_menu_ids);
    }

    /**
     * 获取用户组下拉选项
     * @return array[]
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function getUserGroupOption()
    {
        $row = self::select()->toArray();
        $option = [[
            'value' => 0,
            'label' => "请选择",
            'disabled' => true
        ]];
        foreach ($row as $key => $value) {
            $option[] = [
                'value' => $value['id'],
                'label' => $value['group_name']
            ];
        }
        return $option;
    }

}
